/**
 * <copyright>
 *
 * Copyright (c) 2011 Zeligsoft (2009) Limited
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 
 *   Zeligsoft (2009) Limited - Initial API and implementation
 *
 * </copyright>
 *
 */
package org.omg.deployment.parts.forms;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.eef.runtime.api.component.IPropertiesEditionComponent;
import org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionEvent;
import org.eclipse.emf.eef.runtime.api.parts.IFormPropertiesEditionPart;
import org.eclipse.emf.eef.runtime.impl.notify.PropertiesEditionEvent;
import org.eclipse.emf.eef.runtime.impl.parts.CompositePropertiesEditionPart;
import org.eclipse.emf.eef.runtime.ui.parts.PartComposer;
import org.eclipse.emf.eef.runtime.ui.parts.sequence.BindingCompositionSequence;
import org.eclipse.emf.eef.runtime.ui.parts.sequence.CompositionSequence;
import org.eclipse.emf.eef.runtime.ui.utils.EditingUtils;
import org.eclipse.emf.eef.runtime.ui.widgets.referencestable.ReferencesTableContentProvider;
import org.eclipse.emf.eef.runtime.ui.widgets.referencestable.ReferencesTableSettings;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.omg.deployment.Requirement;
import org.omg.deployment.parts.DeploymentViewsRepository;
import org.omg.deployment.parts.RequirementsPropertiesEditionPart;
import org.omg.deployment.providers.DeploymentMessages;


/**
 * @author Toby McClean; Zeligsoft (2009) Limited
 * @generated
 */
public class RequirementsPropertiesEditionPartForm extends CompositePropertiesEditionPart implements IFormPropertiesEditionPart, RequirementsPropertiesEditionPart {

		protected TableViewer deploymentRequirement;
		protected List<ViewerFilter> deploymentRequirementBusinessFilters = new ArrayList<ViewerFilter>();
		protected List<ViewerFilter> deploymentRequirementFilters = new ArrayList<ViewerFilter>();



	/**
	 * Default constructor
	 * @param editionComponent the {@link IPropertiesEditionComponent} that manage this part
	 * @generated
	 */
	public RequirementsPropertiesEditionPartForm(IPropertiesEditionComponent editionComponent) {
    super(editionComponent);
  }

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.runtime.api.parts.IFormPropertiesEditionPart#
	 *  createFigure(org.eclipse.swt.widgets.Composite, org.eclipse.ui.forms.widgets.FormToolkit)
	 * @generated
	 */
	public Composite createFigure(final Composite parent, final FormToolkit widgetFactory) {
    ScrolledForm scrolledForm = widgetFactory.createScrolledForm(parent);
    Form form = scrolledForm.getForm();
    view = form.getBody();
    GridLayout layout = new GridLayout();
    layout.numColumns = 3;
    view.setLayout(layout);
    createControls(widgetFactory, view);
    return scrolledForm;
  }

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.runtime.api.parts.IFormPropertiesEditionPart#
	 *  createControls(org.eclipse.ui.forms.widgets.FormToolkit, org.eclipse.swt.widgets.Composite)
	 * @generated
	 */
	public void createControls(final FormToolkit widgetFactory, Composite view) {
    CompositionSequence requirementsStep = new BindingCompositionSequence(propertiesEditionComponent);
    requirementsStep.addStep(DeploymentViewsRepository.Requirements.deploymentRequirement);
    
    composer = new PartComposer(requirementsStep) {

      @Override
      public Composite addToPart(Composite parent, Object key) {
        if (key == DeploymentViewsRepository.Requirements.deploymentRequirement) {
          return createDeploymentRequirementTableComposition(widgetFactory, parent);
        }
        return parent;
      }
    };
    composer.compose(view);
  }
	/**
	 * @param container
	 * @generated NOT
	 */
	protected Composite createDeploymentRequirementTableComposition(FormToolkit widgetFactory, Composite container) {
    Composite tableContainer = widgetFactory.createComposite(container, SWT.NONE);
    GridLayout tableContainerLayout = new GridLayout();
    GridData tableContainerData = new GridData(GridData.FILL_BOTH);
    tableContainerData.horizontalSpan = 3;
    tableContainer.setLayoutData(tableContainerData);
    tableContainerLayout.numColumns = 2;
    tableContainer.setLayout(tableContainerLayout);
    org.eclipse.swt.widgets.Table tableDeploymentRequirement = widgetFactory.createTable(tableContainer, SWT.FULL_SELECTION | SWT.BORDER);
    tableDeploymentRequirement.setHeaderVisible(true);
    tableDeploymentRequirement.setEnabled(true);
    tableDeploymentRequirement.setToolTipText(DeploymentMessages.Requirements_ReadOnly);
    GridData gdDeploymentRequirement = new GridData();
    gdDeploymentRequirement.grabExcessHorizontalSpace = true;
    gdDeploymentRequirement.horizontalAlignment = GridData.FILL;
    gdDeploymentRequirement.grabExcessVerticalSpace = true;
    gdDeploymentRequirement.verticalAlignment = GridData.FILL;
    tableDeploymentRequirement.setLayoutData(gdDeploymentRequirement);
    tableDeploymentRequirement.setLinesVisible(true);

    TableColumn name = new TableColumn(tableDeploymentRequirement, SWT.NONE | SWT.RESIZE);
	name.setWidth(80);
	name.setText("Name"); //$NON-NLS-1$
	
	TableColumn resourceType = new TableColumn(tableDeploymentRequirement, SWT.READ_ONLY | SWT.FILL);
	resourceType.setWidth(80);
	resourceType.setText("Type");
    

    deploymentRequirement = new TableViewer(tableDeploymentRequirement);
    deploymentRequirement.setContentProvider(new ArrayContentProvider());
    deploymentRequirement.setLabelProvider(new ITableLabelProvider() {
            public String getColumnText(Object object, int columnIndex) {
              if (object instanceof EObject) {
                switch (columnIndex) {
                case 0:
					return ((Requirement) object).getName();
				case 1:
					return ((Requirement) object).getResourceType();
                }
              }
              return ""; //$NON-NLS-1$
            }
      
            public Image getColumnImage(Object element, int columnIndex) {
              return null;
            }
      

      public void addListener(ILabelProviderListener listener) {
      }

      public void dispose() {
      }

      public boolean isLabelProperty(Object element, String property) {
        return false;
      }

      public void removeListener(ILabelProviderListener listener) {
      }

    });
    deploymentRequirement.getTable().addListener(SWT.MouseDoubleClick, new Listener(){

      public void handleEvent(Event event) {
        if (deploymentRequirement.getSelection() instanceof IStructuredSelection) {
          IStructuredSelection selection = (IStructuredSelection) deploymentRequirement.getSelection();
          if (selection.getFirstElement() instanceof EObject) {
            propertiesEditionComponent.firePropertiesChanged(new PropertiesEditionEvent(RequirementsPropertiesEditionPartForm.this, DeploymentViewsRepository.Requirements.deploymentRequirement, PropertiesEditionEvent.COMMIT, PropertiesEditionEvent.EDIT, null, selection.getFirstElement()));
            deploymentRequirement.refresh();
          }
        }
      }

    });
    GridData deploymentRequirementData = new GridData(GridData.FILL_HORIZONTAL);
    deploymentRequirementData.minimumHeight = 120;
    deploymentRequirementData.heightHint = 120;
    deploymentRequirement.getTable().setLayoutData(deploymentRequirementData);
    for (ViewerFilter filter : this.deploymentRequirementFilters) {
      deploymentRequirement.addFilter(filter);
    }
    EditingUtils.setID(deploymentRequirement.getTable(), DeploymentViewsRepository.Requirements.deploymentRequirement);
    EditingUtils.setEEFtype(deploymentRequirement.getTable(), "eef::TableComposition::field"); //$NON-NLS-1$
    createDeploymentRequirementPanel(widgetFactory, tableContainer);
    return container;
  }

	/**
	 * @param container
	 * @generated NOT
	 */
	protected Composite createDeploymentRequirementPanel(FormToolkit widgetFactory, Composite container) {
    Composite deploymentRequirementPanel = widgetFactory.createComposite(container, SWT.NONE);
    GridLayout deploymentRequirementPanelLayout = new GridLayout();
    deploymentRequirementPanelLayout.numColumns = 1;
    deploymentRequirementPanel.setLayout(deploymentRequirementPanelLayout);
    return deploymentRequirementPanel;
  }



	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionListener#firePropertiesChanged(org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionEvent)
	 * @generated
	 */
	public void firePropertiesChanged(IPropertiesEditionEvent event) {
  }



	/**
	 * {@inheritDoc}
	 * 
	 * @see org.omg.deployment.parts.RequirementsPropertiesEditionPart#initDeploymentRequirement(EObject current, EReference containingFeature, EReference feature)
	 */
	public void initDeploymentRequirement(ReferencesTableSettings settings) {
		if (current.eResource() != null && current.eResource().getResourceSet() != null)
			this.resourceSet = current.eResource().getResourceSet();
		ReferencesTableContentProvider contentProvider = new ReferencesTableContentProvider();
		deploymentRequirement.setContentProvider(contentProvider);
		deploymentRequirement.setInput(settings);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.omg.deployment.parts.RequirementsPropertiesEditionPart#updateDeploymentRequirement()
	 * @generated
	 */
	public void updateDeploymentRequirement() {
  deploymentRequirement.refresh();
}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.omg.deployment.parts.RequirementsPropertiesEditionPart#addFilterDeploymentRequirement(ViewerFilter filter)
	 * @generated
	 */
	public void addFilterToDeploymentRequirement(ViewerFilter filter) {
    deploymentRequirementFilters.add(filter);
    if (this.deploymentRequirement != null) {
      this.deploymentRequirement.addFilter(filter);
    }
  }

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.omg.deployment.parts.RequirementsPropertiesEditionPart#addBusinessFilterDeploymentRequirement(ViewerFilter filter)
	 * @generated
	 */
	public void addBusinessFilterToDeploymentRequirement(ViewerFilter filter) {
    deploymentRequirementBusinessFilters.add(filter);
  }

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.omg.deployment.parts.RequirementsPropertiesEditionPart#isContainedInDeploymentRequirementTable(EObject element)
	 * @generated
	 */
	public boolean isContainedInDeploymentRequirementTable(EObject element) {
    return ((ReferencesTableSettings)deploymentRequirement.getInput()).contains(element);
  }




	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.emf.eef.runtime.api.parts.IPropertiesEditionPart#getTitle()
	 * @generated
	 */
	public String getTitle() {
    return DeploymentMessages.Requirements_Part_Title;
  }



}
